package edu.ccut.saturn.studio.wfg.part;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionLocator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEditPolicy;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
import org.eclipse.gef.requests.GroupRequest;

import edu.ccut.saturn.studio.logic.model.BasicShape;
import edu.ccut.saturn.studio.wfg.command.ConnectionDeleteCommand;
import edu.ccut.saturn.studio.wfg.command.OpenConnectionCommand;
import edu.ccut.saturn.studio.wfg.model.Connection;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnRouterNode;

class ConnectionEditPart extends AbstractConnectionEditPart implements
		PropertyChangeListener {
	
	public void activate() {
		if (!isActive()) {
			super.activate();
			((BasicShape) getModel()).addPropertyChangeListener(this);
		}
	}

	protected void createEditPolicies() {

		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
				new ConnectionEndpointEditPolicy());
		installEditPolicy(EditPolicy.CONNECTION_ROLE,
				new ConnectionEditPolicy() {
					protected Command getDeleteCommand(GroupRequest request) {
						return new ConnectionDeleteCommand(getCastedModel());
					}
				});
	}

	protected IFigure createFigure() {
		PolylineConnection polylineConnection = (PolylineConnection) super.createFigure();
		polylineConnection.setTargetDecoration(new PolygonDecoration());
		polylineConnection.setLineStyle(getCastedModel().getLineStyle());
		polylineConnection.setForegroundColor(getCastedModel().getColor());
		if (getCastedModel().getLineName() != null && getCastedModel().getSource() instanceof SaturnRouterNode ) {
			if("".equals(getCastedModel().getLineName())){
				int i = ((SaturnRouterNode)this.getCastedModel().getSource()).getRouterList().size();
				String lineNameTemp = ((SaturnRouterNode)this.getCastedModel().getSource()).getRouterList().get(i-1).getLineName();
				getCastedModel().setLineName(lineNameTemp);
			}
			Label connLabel = new Label(getCastedModel().getLineName());
			connLabel.setBorder(new LineBorder());
			connLabel.setOpaque(true);
			connLabel.setBackgroundColor(ColorConstants.tooltipBackground);
			polylineConnection.add(connLabel,
					new ConnectionLocator(polylineConnection,
							ConnectionLocator.MIDDLE));
			
		}
		return polylineConnection;
	}

	public void deactivate() {
		if (isActive()) {
			super.deactivate();
			((BasicShape) getModel()).removePropertyChangeListener(this);
		}
	}

	public void performRequest(Request req) {
		if (req.getType() == RequestConstants.REQ_OPEN) {
			getViewer().getEditDomain().getCommandStack().execute(
					new OpenConnectionCommand(((Connection) getModel()).getDialog(), (Connection) getModel()));
		}
	}

	private Connection getCastedModel() {
		return (Connection) getModel();
	}

	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getPropertyName();
		if (Connection.LINESTYLE_PROP.equals(property)) {
			((PolylineConnection) getFigure()).setLineStyle(getCastedModel().getLineStyle());

		} else if (Connection.LINE_NAME_PROPERTY.equals(property)) {
			refreshConnectionFigure();
		}

	}

	private void refreshConnectionFigure() {
		Label connLabel;
		connLabel = new Label(getCastedModel().getLineName());
		connLabel.setBorder(new LineBorder());
		connLabel.setOpaque(true);
		connLabel.setBackgroundColor(ColorConstants.tooltipBackground);
		List<?> d = ((PolylineConnection) getFigure()).getChildren();
		for (int i = 0; i < d.size(); i++) {
			if (d.get(i) instanceof Label) {
				((PolylineConnection) getFigure()).remove((IFigure) d.get(i));
			}
		}
		((PolylineConnection) getFigure()).add(connLabel,
				new ConnectionLocator(((PolylineConnection) getFigure()),
						ConnectionLocator.MIDDLE));

	}

}